"""
Problem 36: https://projecteuler.net/problem=36

Double-base palindromes

The decimal number, 585(10) = 1001001001(2), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in
base 10 and base 2.

(Please note that the palindromic number, in either base, may not include
leading zeros.)
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/12
'''


def isDoublePalindromes(n: int) -> bool:
    '''
    >>> assert isDoublePalindromes(585)
    '''
    ns10 = str(n)
    ns2 = bin(n)[2:]

    if ns10[-1] == '0' or ns2[-1] == '0':
        return False

    return ns10 == ns10[::-1] and ns2 == ns2[::-1]


def solution(limit: int = 1000000) -> int:
    '''
    Find the sum of all numbers, less than one million, 
    which are palindromic in base 10 and base 2.
    '''
    res = 0
    for i in range(1, limit, 2):
        if isDoublePalindromes(i):
            print('DoublePalindromes: ', i)
            res += i

    return res


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # 872187
